package com.platform.sms;

import com.platform.utils.DateUtils;
import com.platform.utils.RRException;
import com.platform.utils.StringUtils;
import com.platform.utils.sms.SmsChuangrui;

import java.util.Date;

/**
 * @Description: 创瑞短信发送
 * @Author: jf2
 * @CreateDate: 2018/5/25 0025 12:11
 * @Version: 1.0
 * Copyright: Copyright (c) 2018
 **/
public class SmsSendChuangruiService extends SmsSendService {

    public SmsSendChuangruiService(SmsSendConfig config) {
        this.smsSendConfig = config;
    }

    @Override
    public void checkConfig() {
        if (StringUtils.isNullOrEmpty(this.smsSendConfig)) {
            throw new RRException("请先配置短信平台信息");
        }
        if (StringUtils.isNullOrEmpty(this.smsSendConfig.getCrDomain())) {
            throw new RRException("请先配置短信平台发送域名");
        }
        if (StringUtils.isNullOrEmpty(this.smsSendConfig.getCrName())) {
            throw new RRException("请先配置短信平台用户名");
        }
        if (StringUtils.isNullOrEmpty(this.smsSendConfig.getCrPwd())) {
            throw new RRException("请先配置短信平台密钥");
        }
        if (StringUtils.isNullOrEmpty(this.smsSendConfig.getCrSign())) {
            throw new RRException("请先配置短信平台签名");
        }
    }

    @Override
    public String sendPost(String mobile,String content,Date stime,String extno) {
        try {
            /**
             * 状态,发送编号,无效号码数,成功提交数,黑名单数和消息，无论发送的号码是多少，一个发送请求只返回一个sendid，如果响应的状态不是“0”，则只有状态和消息
             */
            return SmsChuangrui.crSendSms(this.smsSendConfig.getCrDomain(),this.smsSendConfig.getCrName(), this.smsSendConfig.getCrPwd(), mobile,
                    content, this.smsSendConfig.getCrSign(),
                    DateUtils.format(stime, "yyyy-MM-dd HH:mm:ss"), extno);
        } catch (Exception e) {
            throw new RRException("短信发送失败");
        }
    }

    @Override
    public boolean resultParse(String result) {
        String arr[] = result.split(",");
        if ("0".equals(arr[0])) {
            //发送成功
            return true;
        } else {
            //发送失败
            throw new RRException(arr[1]);
        }
    }
}
